package com.feng.zhihao.apigeteway.gateway.bean.impl.FIELD_TYPE;

import org.springframework.stereotype.Component;

import com.feng.zhihao.apigeteway.entity.InterfaceFieldInfo;
import com.feng.zhihao.apigeteway.gateway.bean.FIELD_TYPE;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;

@Component("FIELD_TYPE_INTEGER")
public class FIELD_TYPE_INTEGER implements FIELD_TYPE{

	@Override
	public String validatorField(InterfaceFieldInfo fieldInfo, Object value) {
		StringBuffer sb=new StringBuffer();
		if(!NumberUtil.isInteger(value.toString())){
			sb.append(StrUtil.format("字段:({}), 不是整数类型:({});\r\n",fieldInfo.getInterfaceFieldName(),value.toString()));
		}
		Integer v=Integer.parseInt(value.toString());
		fieldInfo.getFieldLimitInfos()
		.stream()
		.forEach((fi) -> {
			if(!StrUtil.isBlank(fi.getLimitMax()) && v > Integer.parseInt(fi.getLimitMax())){
				sb.append(StrUtil.format("字段:({}),值:({}), 超过了最大值:({});\r\n",fieldInfo.getInterfaceFieldName(),value.toString(),fi.getLimitMax()));
			}
			if(!StrUtil.isBlank(fi.getLimitMin()) && v < Integer.parseInt(fi.getLimitMin())){
				sb.append(StrUtil.format("字段:({}),值:({}), 小于最小值:({});\r\n",fieldInfo.getInterfaceFieldName(),value.toString(),fi.getLimitMin()));
			}
			if(!StrUtil.isBlank(fi.getLimitEnum())){
				String[] limitEnum=fi.getLimitEnum().split("@fzh@");
				if(!ArrayUtil.contains(limitEnum, value)){
					sb.append(StrUtil.format("字段:({}),值:({}), 不在限制值中:({});\r\n",fieldInfo.getInterfaceFieldName(),value.toString(),ArrayUtil.join(limitEnum, "),(")));
				}
			}
		});
		return sb.toString();
	}

}
